package com.boot.mapreduce;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MiniMapReduceDemo {
    public static void main(String[] args) {
        Job job = new Job();
        job.setMapper(new WordCountMapper());
        job.setReducer(new WordCountReducer());
        job.setInputPath("data/input.txt");

        Coordinator coordinator = new Coordinator();
        coordinator.submitJob(job);
    }

    static class WordCountMapper implements Mapper<Object, String, String, Integer> {
        @Override
        public List<Pair<String, Integer>> map(Object key, String value) {
            List<Pair<String, Integer>> results = new ArrayList<>();
            for (String word : value.split(" ")) {
                results.add(new Pair<>(word, 1));
            }
            return results;
        }
    }

    static class WordCountReducer implements Reducer<String, Integer, String, Integer> {
        @Override
        public List<Pair<String, Integer>> reduce(String keys, List<Integer> values) {
            int sum = values.stream().mapToInt(i -> i).sum();
            return Collections.singletonList(new Pair<>(keys, sum));
        }
    }
}
